﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace D3Calc
{
    public class BNHelper
    {
        private static string DownloadData(string url)
        {
            try
            {
                WebResponse response = ((HttpWebRequest)WebRequest.Create(url)).GetResponse();
                string str = new StreamReader(response.GetResponseStream()).ReadToEnd();
                response.Close();
                if (str == "{\n\"code\" : \"OOPS\",\n\"reason\" : \"There was a problem processing the request.\"\n}")
                {
                    return "error";
                }
                return str;
            }
            catch
            {
                return "";
            }
        }

        public static string GetJson(string url)
        {
            int num = url.LastIndexOf("/");
            if (num == (url.Length - 1))
            {
                num = url.Substring(0, url.Length - 1).LastIndexOf("/");
                url.Substring(num + 1, (url.Length - num) - 2);
            }
            else
            {
                url.Substring(num + 1, (url.Length - num) - 1);
            }
            return DownloadData(url);
        }

        public static string GetValue(string str, string key)
        {
            int index = str.IndexOf(key);
            int num2 = str.IndexOf(",", (int)(index + key.Length));
            if (num2 < 0)
            {
                return str.Substring(index + key.Length).Replace("}", "").Replace("'", "").Replace("\"", "").Trim();
            }
            return str.Substring(index + key.Length, (num2 - index) - key.Length).Replace("'", "").Replace("\"", "").Trim();
        }
    }

}
